﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CMS.ViewModel
{
    //继承ViewModelBase
    public class LoginWindow : ViewModelBase
    {
     
        public AppData AppData { get;private set; }=AppData.Instance;
        //指令写法1
        //private RelayCommand loginCommand;
        //public RelayCommand LoginCommand { get { return loginCommand; } set { loginCommand = value; } }
       public LoginWindow()
        {
            //loginCommand = new RelayCommand(() =>
            //{
            //    MessageBox.Show("登录成功");
            //});
            this.AppData.CurrentUser.Name = "admin";
            this.AppData.CurrentUser.Password = "0";
        }
        
        //写法2
        //登录命令
        public RelayCommand<Window> LoginCommand
        {
            get
            {
                return new RelayCommand<Window>((arg) =>
                {
                  MemberProvider memberProvider=new MemberProvider();
                    var user=memberProvider.Select().FirstOrDefault(item=>item.Name==AppData.CurrentUser.Name&&item.Password==AppData.CurrentUser.Password);
                    if (user==null)
                    {
                        MessageBox.Show("用户名或密码错误");

                    }
                    else
                    {
                        AppData.CurrentUser= user;
                     MainWindow mainWindow = new MainWindow();
                        mainWindow.Show();
                        arg.Close();
                    }
                });
            }
        }
        //关闭命令
        public RelayCommand CloseCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    App.Current.Shutdown();
                });
            }
        }
    }
}
